﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Frm_Recherche_clients

    Private Sub Frm_Recherche_clients_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Frm_Gestion_Clients.Enabled = True
        Frm_Demande_Clients.Enabled = True
        Frm_Modif_Demande_Clients.Enabled = True
        Frm_Demande_Clients_loc.Enabled = True
        Frm_Paiement_Clients.Enabled = True
    End Sub

    Private Sub Frm_Recherche_clients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        comb_selectionnez.SelectedIndex = 0
    End Sub

    Private Sub txt_mot_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mot.TextChanged
        If rech = "client" Or rech = "tarikLoc" Then
            Dim mot As String = ""
            If comb_selectionnez.SelectedItem = "Nom" Then
                mot = "Nom"
            ElseIf comb_selectionnez.SelectedItem = "Prenom" Then
                mot = "Prenom"
            ElseIf comb_selectionnez.SelectedItem = "Cin" Then
                mot = "Cin"
            ElseIf comb_selectionnez.SelectedItem = "Nom de L'entreprise" Then
                mot = "Nom_entreprise"

            ElseIf comb_selectionnez.SelectedItem = "Nom de L'interlocuteur" Then
                mot = "Nom_interlocuteur"
            End If
            Try
                List_tous.Items.Clear()
                Cmd = New SqlCommand("Select * From Clients where " & mot & " like '%" & txt_mot.Text & "%'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Cin"), Dr("Nom"), Dr("Prenom"), Dr("Nom_entreprise"), Dr("Nom_interlocuteur"), Dr("Id_Client")})
                    List_tous.Items.Add(Line)
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            If txt_mot.Text = "" Then
                List_tous.Items.Clear()
            End If
        End If

        If rech = "demande" Or rech = "Modif demande" Then
            Dim mot As String = ""
            If comb_selectionnez.SelectedItem = "Nom" Then
                mot = "Nom"
            ElseIf comb_selectionnez.SelectedItem = "Prenom" Then
                mot = "Prenom"
            ElseIf comb_selectionnez.SelectedItem = "Cin" Then
                mot = "Cin"
            ElseIf comb_selectionnez.SelectedItem = "Nom de L'entreprise" Then
                mot = "Nom_entreprise"

            ElseIf comb_selectionnez.SelectedItem = "Nom de L'interlocuteur" Then
                mot = "Nom_interlocuteur"
            End If
            Try
                List_tous.Items.Clear()
                Cmd = New SqlCommand("Select * From Clients where Location='Non' and " & mot & " like '%" & txt_mot.Text & "%'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Cin"), Dr("Nom"), Dr("Prenom"), Dr("Nom_entreprise"), Dr("Nom_interlocuteur"), Dr("Id_Client")})
                    List_tous.Items.Add(Line)
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            If txt_mot.Text = "" Then
                List_tous.Items.Clear()
            End If
        End If
        If rech = "loc" Or rech = "paiem" Then
            Dim mot As String = ""
            If comb_selectionnez.SelectedItem = "Nom" Then
                mot = "Nom"
            ElseIf comb_selectionnez.SelectedItem = "Prenom" Then
                mot = "Prenom"
            ElseIf comb_selectionnez.SelectedItem = "Cin" Then
                mot = "Cin"
            ElseIf comb_selectionnez.SelectedItem = "Nom de L'entreprise" Then
                mot = "Nom_entreprise"

            ElseIf comb_selectionnez.SelectedItem = "Nom de L'interlocuteur" Then
                mot = "Nom_interlocuteur"
            End If
            Try
                List_tous.Items.Clear()
                Cmd = New SqlCommand("Select * From Clients where Location='Oui' and " & mot & " like '%" & txt_mot.Text & "%'", Con)
                open()
                Dr = Cmd.ExecuteReader()
                Do While Dr.Read()
                    Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Cin"), Dr("Nom"), Dr("Prenom"), Dr("Nom_entreprise"), Dr("Nom_interlocuteur"), Dr("Id_Client")})
                    List_tous.Items.Add(Line)
                Loop
                Dr.Close()
                cnClose()
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                cnClose()
            End Try
            If txt_mot.Text = "" Then
                List_tous.Items.Clear()
            End If
        End If
    End Sub

    Private Sub btn_selectionnez_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_selectionnez.Click

        Dim n As Integer
        With List_tous
            If .SelectedIndices.Count > 0 Then
                n = CInt(.Items(.SelectedIndices(0)).SubItems(5).Text)


                If rech = "client" Then
                    For i As Integer = 0 To Frm_Gestion_Clients.List_tous.Items.Count - 1
                        If Frm_Gestion_Clients.List_tous.Items(i).SubItems(8).Text = n Then
                            Frm_Gestion_Clients.List_tous.Items(i).Focused = True
                            Frm_Gestion_Clients.List_tous.Items(i).Selected = True
                        End If
                    Next
                    Me.Close()
                ElseIf rech = "tarikLoc" Then
                    For Each it As DictionaryEntry In Frm_Demande_Clients_loc.cmbClient.Items
                        If it.Key = n Then
                            Frm_Demande_Clients_loc.cmbClient.SelectedItem = it
                            Exit For
                            rech = ""
                        End If
                    Next
                    Me.Close()
                End If
                If rech = "demande" Then
                    Try
                        Cmd = New SqlCommand("Select * From Clients Where Id_Client=" & n, Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        Do While Dr.Read()
                            If Dr("Type_Client") = "Entreprise" Then
                                Frm_Demande_Clients.comb_client.SelectedItem = (Dr("Nom_entreprise"))
                            Else
                                Frm_Demande_Clients.comb_client.SelectedItem = (Dr("Nom") & "  " & Dr("Prenom"))
                            End If
                        Loop
                        Dr.Close()
                        cnClose()

                        Me.Close()
                    Catch ex As Exception

                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                ElseIf rech = "Modif demande" Then
                    Try
                        Cmd = New SqlCommand("Select * From Clients Where Id_Client=" & n, Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        Do While Dr.Read()
                            If Dr("Type_Client") = "Entreprise" Then
                                Frm_Modif_Demande_Clients.comb_client.SelectedItem = (Dr("Nom_entreprise"))
                            Else
                                Frm_Modif_Demande_Clients.comb_client.SelectedItem = (Dr("Nom") & "  " & Dr("Prenom"))
                            End If
                        Loop
                        Dr.Close()
                        cnClose()

                        Me.Close()
                    Catch ex As Exception

                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                ElseIf rech = "loc" Then
                    Try
                        Dim str As String = ""
                        Cmd = New SqlCommand("Select * From Clients Where Id_Client=" & n, Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        Do While Dr.Read()
                            If Dr("Type_Client") = "Entreprise" Then
                                str = (Dr("Nom_entreprise"))
                            Else
                                str = (Dr("Nom") & "  " & Dr("Prenom"))
                            End If
                        Loop
                        Dr.Close()
                        cnClose()

                        Me.Close()
                        Frm_Demande_Clients_loc.cmbClient.SelectedItem = str
                    Catch ex As Exception

                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                ElseIf rech = "paiem" Then
                    Try
                        Dim str As String = ""
                        Cmd = New SqlCommand("Select * From Clients Where Id_Client=" & n, Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        Do While Dr.Read()
                            If Dr("Type_Client") = "Entreprise" Then
                                str = (Dr("Nom_entreprise"))
                            Else
                                str = (Dr("Nom") & "  " & Dr("Prenom"))
                            End If
                        Loop
                        Dr.Close()
                        cnClose()

                        Me.Close()
                        Frm_Paiement_Clients.comb_client.SelectedItem = str
                    Catch ex As Exception

                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                End If


            Else
                MsgBox("Vous n'avez selectionner aucun Client !", MsgBoxStyle.Critical, "Réssayer !")
            End If

        End With
    End Sub

    Private Sub comb_selectionnez_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles comb_selectionnez.KeyPress
        e.Handled = True
    End Sub

    Private Sub comb_selectionnez_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comb_selectionnez.SelectedIndexChanged
        txt_mot.Clear()
    End Sub
End Class